package com.woniuxy.purchase.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.purchase.entity.dto.AddSupplierDTO;
import com.woniuxy.purchase.entity.dto.SelectPurchaseDTO;
import com.woniuxy.purchase.entity.pojo.BuyPurchase;
import com.woniuxy.purchase.entity.vo.PurchaseDetailsVO;
import com.woniuxy.purchase.service.BuyPurchaseService;
import com.woniuxy.purchase.util.R;
import com.woniuxy.response.CustomPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/purchase/buyPurchase")
@Slf4j
public class BuyPurchaseController {

    @Resource
    private BuyPurchaseService buyPurchaseService;
    /**
     * 为采购单指定供应商
     */
    @PostMapping("/insertSupplierToPurchase")
    @PreAuthorize("hasAuthority('buy:updateBuy')")
    public R insertSupplierToPurchase(@RequestBody AddSupplierDTO dto){
        log.info("开始为采购单添加供应商,{}",dto);
        buyPurchaseService.updatePurchaseById(dto);
        return R.success();
    }

    /**
     * 修改采购单状态
     */
    @GetMapping("/updateState/{state}/id/{id}")
    @PreAuthorize("hasAuthority('buy:updateBuy')")
    public R updatePurchaseStateById(@PathVariable("state")Integer state,@PathVariable("id")Integer id){
        buyPurchaseService.updatePurchaseStateById(state,id);
        return R.success();
    }

    /**
     * 根据状态查询采购单
     */
    @PostMapping("/findPurchaseByCondition")
    public R<List<BuyPurchase>> findPurchaseByCondition(@RequestBody SelectPurchaseDTO dto){
        log.info("开始查询采购单,{}",dto);
        Page<BuyPurchase> page = buyPurchaseService.findPurchaseByCondition(dto);
        CustomPage<List<BuyPurchase>> customPage = new CustomPage<>();
        //每页多少条数据
        customPage.setSize((int) page.getSize());
        //当前页码
        customPage.setCurrentPage((int) page.getCurrent());
        //总页数
        customPage.setTotalPage((int) page.getPages());
        customPage.setTotal((int)page.getTotal());
        //数据
        customPage.setData(page.getRecords());
        return R.success(customPage);
    }

    /**
     * 根据需求单ID查询包含哪些商品
     * end
     */
    @GetMapping("/findPurchaseDetails/{id}")
    public R<List<PurchaseDetailsVO>> findDemandDetails(@PathVariable("id")Integer id){
        return R.success(buyPurchaseService.findDemandDetails(id));
    }

    @GetMapping("/findAllPurchase/{id}")
    public R findAllPurchase(@PathVariable("id")Integer id){
        return R.success(buyPurchaseService.findAllPurchase(id));
    }

}